package com.pdsu.graduation_design.views;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.pdsu.graduation_design.MainActivity;
import com.pdsu.graduation_design.R;
import com.pdsu.graduation_design.adapter.PayOrderSelectTimeAdapter;
import com.pdsu.graduation_design.utils.JsonUtil;
import com.pdsu.graduation_design.utils.Result;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

public class PayMoneyActivity extends AppCompatActivity {

    private TextView all_price,pay_now_heji,pay_money_tv;
    Context context;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pay_money);
        init();
        bind();
    }
    void init(){

        pay_now_heji=findViewById(R.id.pay_now_heji);
        all_price=findViewById(R.id.pay_now_allprice);
        pay_money_tv=findViewById(R.id.pay_money_tv);

        context=this;

    }
    void bind(){

        String allprice=getIntent().getStringExtra("yingzhifu");
        String order_number=getIntent().getStringExtra("order_number");
        System.out.println(allprice+"allprice");
        all_price.setText(allprice);
        pay_now_heji.setText(allprice);
        pay_money_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                BottomSheetDialog dialog = new BottomSheetDialog(context);
                System.out.println(context);
                View view = getLayoutInflater().inflate(R.layout.dialog_payment, null);

                EditText passwordEditText=view.findViewById(R.id.password_edit_text);
                Button confirmButton = view.findViewById(R.id.confirm_button);
                confirmButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String password = passwordEditText.getText().toString();
                        if (password.length() == 6) {
                            // 调用支付接口进行验证
                            RequestParams requestParams= new RequestParams(MainActivity.URL+"/order/payorder");
                            requestParams.addParameter("order_number",order_number);
                            x.http().post(requestParams, new Callback.CommonCallback<String>() {
                                @Override
                                public void onSuccess(String result) {

                                    try {
                                        Result r = JsonUtil.JsonToObj(result,Result.class);

                                        if (r.getCode().equals("200")){
                                            Toast.makeText(context,"支付成功",Toast.LENGTH_SHORT).show();
                                            dialog.dismiss();
                                            finish();
                                        }else {
                                            Toast.makeText(context,"支付失败",Toast.LENGTH_SHORT).show();
                                            dialog.dismiss();
                                            finish();
                                        }

                                    } catch (JsonProcessingException e) {
                                        e.printStackTrace();
                                    }

                                }

                                @Override
                                public void onError(Throwable ex, boolean isOnCallback) {

                                }

                                @Override
                                public void onCancelled(CancelledException cex) {

                                }

                                @Override
                                public void onFinished() {

                                }
                            });



                        } else {
                            Toast.makeText(context, "请输入6位支付密码", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
                dialog.setContentView(view);
                dialog.show();
//                PaymentDialog dialog = new PaymentDialog(context);
//                dialog.show();
            }
        });
    }
}